TARGET = gateway
DEBUG = 1
OPT = -Og

BUILD_DIR = build

C_SOURCES =  \
app/main.c \
app/syscalls.c \
app/stm32f7xx_it.c \
app/system_stm32f7xx.c \
app/initTask.c \
app/guixTask.c \
app/filesystem.c \
app/tx_initialize_low_level.c \
app/util.c \
driver/drvLcd.c \
driver/drvLog.c \
driver/drvClk.c \
driver/drvSdram.c \
driver/drvSD.c \
driver/drvFs.c \
driver/drvInternelFlash.c \
driver/drvExtflash.c \
hal/stm32f7xx_hal_cortex.c \
hal/stm32f7xx_hal_tim.c \
hal/stm32f7xx_hal_tim_ex.c \
hal/stm32f7xx_hal_uart.c \
hal/stm32f7xx_hal_uart_ex.c \
hal/stm32f7xx_hal_rcc.c \
hal/stm32f7xx_hal_rcc_ex.c \
hal/stm32f7xx_hal_flash.c \
hal/stm32f7xx_hal_flash_ex.c \
hal/stm32f7xx_hal_gpio.c \
hal/stm32f7xx_hal_dma.c \
hal/stm32f7xx_hal_dma_ex.c \
hal/stm32f7xx_hal_pwr.c \
hal/stm32f7xx_hal_pwr_ex.c \
hal/stm32f7xx_hal.c \
hal/stm32f7xx_hal_i2c.c \
hal/stm32f7xx_hal_i2c_ex.c \
hal/stm32f7xx_hal_exti.c \
hal/stm32f7xx_hal_sdram.c \
hal/stm32f7xx_hal_ltdc.c \
hal/stm32f7xx_ll_fmc.c \
hal/stm32f7xx_hal_mmc.c \
hal/stm32f7xx_hal_sd.c \
hal/stm32f7xx_ll_sdmmc.c \
hal/stm32f7xx_hal_qspi.c \
hal/stm32f7xx_hal_timebase_tim_template.c

ASM_SOURCES =  \
app/startup_stm32f750xx.s

C_INCLUDES =  \
-Iapp \
-Idriver \
-Ihal \
-Ihal/Legacy \
-Icmsis \
-Ithreadx/include/threadx \
-Ithreadx/include/guix \
-Ithreadx/include/filex \
-Ithreadx/ports

C_DEFS =  \
-DUSE_HAL_DRIVER \
-DSTM32F750xx \
-DTX_ENABLE_FPU_SUPPORT \
-DTX_ENABLE_STACK_CHECKING \
-DTX_INCLUDE_USER_DEFINE_FILE \
-DUSE_FULL_LL_DRIVER

AS_INCLUDES = 

AS_DEFS = \
-DTX_ENABLE_FPU_SUPPORT

PREFIX = arm-none-eabi-
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

CPU = -mcpu=cortex-m7
FPU = -mfpu=fpv5-sp-d16
FLOAT-ABI = -mfloat-abi=hard
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif

CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

LDSCRIPT = STM32F750N8Hx_FLASH.ld

LIBDIR = -Lthreadx/lib
LIBS = -lc -lm -lnosys -lguix -lusbx -lnetxduo -lfilex -llevelx -lthreadx
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex

# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@

$(BUILD_DIR):
	mkdir $@
clean:
	-rm -fR $(BUILD_DIR)

-include $(wildcard $(BUILD_DIR)/*.d)
